package com.kyzh.core.pager.weal.vip

import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.beans.VipCentreBottomBean
import com.gushenge.core.impls.VipImpl
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemVipcentreContent1Binding
import com.kyzh.core.databinding.ItemVipcentreContentBinding
import com.kyzh.core.dialog.hideKeyboard
import com.kyzh.core.dialog.showSimpleDialog
import com.gushenge.core.base.fragment.BaseFragment
import com.kyzh.core.uis.DividerItemDecoration
import com.kyzh.core.utils.loadImage
import com.kyzh.core.utils.setVisibility
import kotlinx.android.synthetic.main.fragment_vipcentre.*
import org.jetbrains.anko.backgroundResource

class VipCentreFragment : BaseFragment(){
    companion object{
        fun newInstance(type : Int): VipCentreFragment {
            val args = Bundle().apply {
                putInt("type",type)
            }

            val fragment = VipCentreFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_vipcentre, container, false)
    }
    private var mP :Int = 1
    private var type :Int? = 0
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        type = arguments?.getInt("type")
        rvList.layoutManager = LinearLayoutManager(requireContext())
        rvList.adapter =  adapter
        adapter.setEmptyView(R.layout.empty)
        header()
        getData1(null)
        swipe.setOnRefreshListener {
            mP = 1
            VipImpl.getVipGift(null,mP){
                this?.let {
                    it.data?.let { data ->
                        datas = data
                        adapter.setNewInstance(datas)
                        if(it.p>=it.max_p){
                            adapter.loadMoreModule.loadMoreEnd(true)
                        }
                    }
                }
                swipe.isRefreshing = false
            }
        }
        adapter.loadMoreModule.setOnLoadMoreListener {
            mP++
            VipImpl.getVipGift(null,mP){
                this?.let {
                    it.data?.let { data ->
                        datas?.addAll(data)
                        adapter.addData(data)
                        adapter.loadMoreModule.loadMoreComplete()
                        if(it.p>=it.max_p){
                            adapter.loadMoreModule.loadMoreEnd(true)
                        }
                    }
                }
            }
        }
    }
    fun getData1(string:String?){
        mP = 1
        VipImpl.getVipGift(string,mP){
            this?.let {
                it.data?.let { data ->
                    datas = data
                    adapter.setNewInstance(datas)
                    if(it.p>=it.max_p){
                        adapter.loadMoreModule.loadMoreEnd(true)
                    }
                }
            }
        }
    }
    //头部相关
    private fun header(){
        val search = heade.findViewById<EditText>(R.id.search)
        val ivSearch = heade.findViewById<ImageView>(R.id.ivSearch)
        val ivClean = heade.findViewById<ImageView>(R.id.ivClean)
        ivClean.setOnClickListener {
            requireActivity().hideKeyboard(search)
            search.setText("")
            getData1(null)
        }
        ivSearch.setOnClickListener {
            requireActivity().hideKeyboard(search)
            getData1(search.text.toString().trim())
        }
        search.setOnEditorActionListener { v, actionId, event ->
            if(actionId == EditorInfo.IME_ACTION_SEARCH){
                requireActivity().hideKeyboard(search)
                val searchValue = search.text.toString().trim()
                if(TextUtils.isEmpty(searchValue)){
                    getData1(null)
                }else{
                    search.clearFocus()
                    getData1(searchValue)
                }
            }
            return@setOnEditorActionListener false
        }
        search.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val searchValue = search.text.toString().trim()
                if(TextUtils.isEmpty(searchValue)){
                    getData1(null)
                    ivClean.setVisibility(false)
                }else{
                    ivClean.setVisibility(true)
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })
    }

    override fun onResume() {
        super.onResume()
        getData1(null)
    }
    private var datas :ArrayList<VipCentreBottomBean>? = arrayListOf()
    private val adapter=object :BaseQuickAdapter<VipCentreBottomBean,BaseDataBindingHolder<ItemVipcentreContentBinding>>(R.layout.item_vipcentre_content),LoadMoreModule{
        override fun convert(
            holder1: BaseDataBindingHolder<ItemVipcentreContentBinding>,
            item: VipCentreBottomBean
        ) {
            holder1.dataBinding?.apply {
                icon.loadImage(item.icon,10)
                tvName.text = item.gname
                revItem.layoutManager = LinearLayoutManager(requireContext())
                if(revItem.itemDecorationCount==0){
                    revItem.addItemDecoration(DividerItemDecoration(resources.getDrawable(R.drawable.dasher_line),30))
                }
                revItem.adapter = object  : BaseQuickAdapter<VipCentreBottomBean.Gift,BaseDataBindingHolder<ItemVipcentreContent1Binding>>(R.layout.item_vipcentre_content_1,item.list){
                    override fun convert(
                        holder: BaseDataBindingHolder<ItemVipcentreContent1Binding>,
                        item: VipCentreBottomBean.Gift
                    ) {
                        holder.setIsRecyclable( false)
                        holder.dataBinding?.data = item
                        holder.dataBinding?.apply {
//                            0 未领取 1 已领取 2不允许领取
                            when (item.status) {
                                0 -> {
                                    tvGet.text = "领取"
                                    tvGet.setTextColor(Color.parseColor("#4F3200"))
                                    tvGet.isEnabled = true
                                    tvGet.backgroundResource = R.drawable.bg_btn_open
                                }
                                1 -> {
                                    tvGet.text = "已领取"
                                    tvGet.setTextColor(Color.parseColor("#666666"))
                                    tvGet.isEnabled = true
                                    tvGet.backgroundResource = R.drawable.bg_btn_unopen
                                }
                                else -> {
                                    tvGet.text = "未领取"
                                    tvGet.setTextColor(Color.parseColor("#666666"))
                                    tvGet.isEnabled = false
                                    tvGet.backgroundResource = R.drawable.bg_btn_unopen
                                }
                            }

                            tvGet.setOnClickListener {
                                if(item.status == 0){
                                    VipImpl.setVipHeaderItem(type,item.id){
                                        if(!TextUtils.isEmpty(this)){
                                            datas!!.get(holder1.position)!!.list!!.get(holder.position)!!.status =1
                                            notifyItemChanged(holder.position)
                                        }
                                        this?.apply {
                                            showSimpleDialog("查看结果",this,"确定")
                                        }
                                    }
                                }else{
                                    VipImpl.setVipHeaderItem(type,item.id){
                                        this?.apply {
                                            showSimpleDialog("查看结果",this,"确定")
                                        }
                                    }

                                }
                            }
                        }
                    }

                }
            }
        }

    }
}